package com.zz.cc.owner.app;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

import com.zz.cc.owner.R;
import com.zz.cc.owner.TitleBaseActivity;
import com.zz.cc.owner.data.CarModel;
import com.zz.cc.owner.data.CarSubModel;
import com.zz.common.utils.ZLog;
import com.zz.common.widget.ZExpandableListView;

public class RegisterCarModelActivity extends TitleBaseActivity implements OnGroupClickListener
					, OnChildClickListener{
	private static final String TAG = "RegisterCarModelActivity";
	
	private ZExpandableListView mListView;
	private CarModelExpandableAdapter mAdapter;

	private final ArrayList<CarModel> mModels = new ArrayList<CarModel>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_car_model);
		initUI();
		fillView();
	}
	
	private void initUI() {
		mListView = (ZExpandableListView) findViewById(R.id.car_model_list_view);
	}
	
	private void fillView() {
		setCurrentTitle(R.string.card_model_title);
		mListView.setOnChildClickListener(this);
		mListView.setOnGroupClickListener(this);
		mAdapter = new CarModelExpandableAdapter();
		mListView.setAdapter(mAdapter);
	}

	@Override
	public boolean onChildClick(ExpandableListView listview, View view, int groupPosition,
			int childPosition, long id) {
		ViewHolder holder = (ViewHolder) view.getTag();
		if(null != holder) {
			ZLog.d(TAG, "onClickClick: " + groupPosition + ", " + childPosition + ", "
					+ holder.mSubModel.mSubModel + ", " + holder.mSubModel.mSubModelId);
			Intent intent = new Intent(this, RegisterPhoneNumActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
		}
		return true;
	}

	@Override
	public boolean onGroupClick(ExpandableListView listview, View view, int groupPosition,
			long id) {
		ViewHolder holder = (ViewHolder) view.getTag();
		if(null != holder) {
			ZLog.d(TAG, "onGroupClick: " + groupPosition + ", " + holder.mModel.mModel + ", " + holder.mModel.mModelId);
		}
		return false;
	}
	
	private static class ViewHolder {
		TextView mTvName;
		
		CarModel mModel;
		CarSubModel mSubModel;
	}
	
	private class CarModelExpandableAdapter extends BaseExpandableListAdapter {

		@Override
		public CarSubModel getChild(int groupPosition, int childPosition) {
			CarModel model = getGroup(groupPosition);
			return model.getSubModelList().get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			CarSubModel subModel = getChild(groupPosition, childPosition);
			return 0;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = LayoutInflater.from(RegisterCarModelActivity.this).inflate(R.layout.layout_car_model_item, null);
				holder = new ViewHolder();
				holder.mTvName = (TextView) convertView.findViewById(R.id.car_model_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			CarSubModel subModel = getChild(groupPosition, childPosition);
			holder.mTvName.setText(subModel.mSubModel);
			holder.mSubModel = subModel;
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			CarModel model = getGroup(groupPosition);
			if(null == model.getSubModelList()) {
				return 0;
			} 
			return model.getSubModelList().size();
		}

		@Override
		public CarModel getGroup(int groupPosition) {
			return mModels.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return mModels.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			CarModel model = getGroup(groupPosition);
			return 0;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = LayoutInflater.from(RegisterCarModelActivity.this).inflate(R.layout.layout_car_brank_header_item, null);
				holder = new ViewHolder();
				holder.mTvName = (TextView) convertView.findViewById(R.id.car_sub_model_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			CarModel model = getGroup(groupPosition);
			holder.mTvName.setText(model.mModel);
			holder.mModel = model;
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}
		
	}
}
